package com.csgc.camera.biz.other.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.csgc.camera.bean.LocationInfo
import com.csgc.camera.util.clickWithTrigger
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ItemLocationSelectBinding




class LocationSelectAdapter(val context:Context,val list:ArrayList<LocationInfo>):Adapter<LocationSelectAdapter.LocationSelectHolder>() {


    companion object {
        private const val TEXT_USE_WATERMARK = "使用水印"
        private const val TEXT_CANCEL_WATERMARK = "取消水印"
    }

    private var checkPosition = 0
    var listener:OnLocationSelectListener?=null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocationSelectHolder {
        return LocationSelectHolder(ItemLocationSelectBinding.inflate(LayoutInflater.from(context),parent,false))
    }

    override fun onBindViewHolder(holder: LocationSelectHolder, @SuppressLint("RecyclerView") position: Int) {
        val data = list[position]
        holder.binding.apply {
            tvLocationRoad.text = "${data.province}${data.city}${data.district}${data.street}"
            tvSnippet.text = data.snippet

            // 统一处理按钮状态
            val isCheckPosition = position == checkPosition
            tvUse.text = if (isCheckPosition) TEXT_CANCEL_WATERMARK else TEXT_USE_WATERMARK
            tvUse.shapeDrawableBuilder
                ?.setSolidColor(context.getColor(if (isCheckPosition) R.color.color_EDEDED else R.color.color_F6FFF1))
                ?.buildBackgroundDrawable()
            tvUse.setTextColor(context.getColor(
                if (isCheckPosition) R.color.title_color else R.color.color_679905
            ))

            root.clickWithTrigger {
                if (position == checkPosition) {
                    checkPosition = 0 // 重置为第一位
                } else {
                    checkPosition = position
                }
                notifyDataSetChanged()

                // 只有在真正选中时才触发回调
                if (checkPosition == position) {
                    listener?.onSelect(data.snippet)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class LocationSelectHolder(val binding:ItemLocationSelectBinding):ViewHolder(binding.root)
    interface OnLocationSelectListener{
        fun onSelect(snippet:String)
    }
}